package com.wing.bill.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.controller.admin.ReportUtil;
import com.wing.bill.dao.TempStorageReportDao;
import com.wing.cabinet.model.response.StorageInventoryInoutVO;
import com.wing.cabinet.model.response.StorageInventorySpaceVO;
import com.wing.cabinet.service.StorageInventoryInoutService;
import com.wing.cabinet.service.StorageInventorySpaceService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.LocalDateTimeUtils;
import com.wing.common.utils.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TempStorageReportServiceImpl implements TempStorageReportService{
    @Autowired
    private TempStorageReportDao tempStorageReportDao;
    @Autowired
    private StorageInventoryInoutService storageInventoryInoutService;

    @Override
    public JsonResult list(String param, String code) {
        HashMap hm = new HashMap();
        hm.put("title","物料存入统计(按天)");
        hm.put("data",tempStorageReportDao.list(ReportUtil.hundredDays().get(0),ReportUtil.hundredDays().get(1),code,param));
        return JsonResult.success(hm);
    }

    @Override
    public JsonResult topCell(String type, String startTime, String endTime, String mainCabCode, String code, Integer click) {
        if(click==0){
        HashMap map = new HashMap(){{put("title","存刀柜数量(支)");put("sign","1");}};
        HashMap map1 = new HashMap(){{put("title","在库sku数量(个)");put("sign","2");}};
        HashMap map2 = new HashMap(){{put("title","在库sku金额(元)");put("sign","3");}};
        HashMap map3 = new HashMap(){{put("title","报修数量(待盘验)(支)");put("sign","4");}};
        HashMap map4 = new HashMap(){{put("title","报废数量(待盘验)(支)");put("sign","5");}};
        HashMap map5 = new HashMap(){{put("title","空库位个数(个)");put("sign","6");}};
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            map.putAll(tempStorageReportDao.topCellOne(start,end,mainCabCode,code));
            map1.putAll(tempStorageReportDao.topCellTwo(start,end,mainCabCode,code));
            map2.putAll(tempStorageReportDao.topCellThree(start,end,mainCabCode,code));
            map3.putAll(tempStorageReportDao.topCellFour(start,end,mainCabCode,code));
            map4.putAll(tempStorageReportDao.topCellFive(start,end,mainCabCode,code));
            map5.putAll(tempStorageReportDao.topCellSix(mainCabCode,code));
        }
        if (type.equals("0")){
            map.putAll(tempStorageReportDao.topCellOne(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
            map1.putAll(tempStorageReportDao.topCellTwo(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
            map2.putAll(tempStorageReportDao.topCellThree(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
            map3.putAll(tempStorageReportDao.topCellFour(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
            map4.putAll(tempStorageReportDao.topCellFive(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
            map5.putAll(tempStorageReportDao.topCellSix(mainCabCode,code));

        }else if (type.equals("1")){
            map.putAll(tempStorageReportDao.topCellOne(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
            map1.putAll(tempStorageReportDao.topCellTwo(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
            map2.putAll(tempStorageReportDao.topCellThree(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
            map3.putAll(tempStorageReportDao.topCellFour(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
            map4.putAll(tempStorageReportDao.topCellFive(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
            map5.putAll(tempStorageReportDao.topCellSix(mainCabCode,code));
        }else if (type.equals("2")){
            map.putAll(tempStorageReportDao.topCellOne(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
            map1.putAll(tempStorageReportDao.topCellTwo(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
            map2.putAll(tempStorageReportDao.topCellThree(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
            map3.putAll(tempStorageReportDao.topCellFour(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
            map4.putAll(tempStorageReportDao.topCellFive(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
            map5.putAll(tempStorageReportDao.topCellSix(mainCabCode,code));
        }
        List<HashMap> list = new ArrayList();
        list.add(map);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        return JsonResult.success("查询成功",list);
        }else{
            if (StringUtils.isNotBlank(type)&&click==1){//存刀柜数量点击 固定时间
                if (type.equals("0")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellOneClick(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),code,mainCabCode));
                }else if (type.equals("1")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellOneClick(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code, mainCabCode));
                }else{
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellOneClick(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code, mainCabCode));
                }
            }
            if (StringUtils.isNotBlank(type)&&click==2){//在库sku数量点击 固定时间
                if (type.equals("0")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellOneClick(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),code,mainCabCode));
                }else if (type.equals("1")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellOneClick(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code, mainCabCode));
                }else{
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellOneClick(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code, mainCabCode));
                }
            }
            if (StringUtils.isNotBlank(type)&&click==3){//在库sku金额点击 固定时间
                if (type.equals("0")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellThreeClick(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),code,mainCabCode));
                }else if (type.equals("1")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellThreeClick(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code, mainCabCode));
                }else{
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellThreeClick(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code, mainCabCode));
                }
            }
            if (StringUtils.isNotBlank(type)&&click==4){//报修数量待盘验点击 固定时间
                if (type.equals("0")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellFourClick(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),code,mainCabCode));
                }else if (type.equals("1")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellFourClick(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code, mainCabCode));
                }else{
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellFourClick(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code, mainCabCode));
                }
            }
            if (StringUtils.isNotBlank(type)&&click==5){//报废数量待盘验点击 固定时间
                if (type.equals("0")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellFiveClick(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),code,mainCabCode));
                }else if (type.equals("1")){
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellFiveClick(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code, mainCabCode));
                }else{
                    return JsonResult.success("查询成功",tempStorageReportDao.topCellFiveClick(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code, mainCabCode));
                }
            }
            if (StringUtils.isNotBlank(type)&&click==6){//空库位个数点击 固定时间
                return JsonResult.success("查询成功",tempStorageReportDao.topCellSixClick(code, mainCabCode));
            }
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)&&click==1){//存刀柜数量点击 指定时间
                return JsonResult.success("查询成功",tempStorageReportDao.topCellOneClick(start,end,code, mainCabCode));
            }
            if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)&&click==2){//在库sku数量点击 指定时间
                return JsonResult.success("查询成功",tempStorageReportDao.topCellOneClick(start,end,code, mainCabCode));
            }
            if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)&&click==3){//在库sku金额点击 指定时间
                return JsonResult.success("查询成功",tempStorageReportDao.topCellThreeClick(start,end,code, mainCabCode));
            }
            if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)&&click==4){//报修数量待盘验点击 指定时间
                return JsonResult.success("查询成功",tempStorageReportDao.topCellFourClick(start,end,code, mainCabCode));
            }
            if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)&&click==5){//报废数量待盘验点击 指定时间
                return JsonResult.success("查询成功",tempStorageReportDao.topCellFiveClick(start,end,code, mainCabCode));
            }
            if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)&&click==6){//空库位个数点击 指定时间
                return JsonResult.success("查询成功",tempStorageReportDao.topCellSixClick(code,mainCabCode));
            }
        }
        return null;
    }

    @Override
    public JsonResult midFirstChart(String type, String startTime, String endTime, String mainCabCode, String code, String param) {
        HashMap map = new HashMap(){{put("title","按人");}};
        List<HashMap> list = new ArrayList();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            list.addAll(tempStorageReportDao.midFirstChart(start,end,mainCabCode,code,param));
        }
        if (type.equals("0")){
            list.addAll(tempStorageReportDao.midFirstChart(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code,param));
        }else if (type.equals("1")){
            list.addAll(tempStorageReportDao.midFirstChart(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code,param));
        }else if (type.equals("2")){
            list.addAll(tempStorageReportDao.midFirstChart(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code,param));
        }
        map.put("data",list);
        return JsonResult.success("查询成功",map);
    }

    @Override
    public JsonResult midSecondChart(String type, String startTime, String endTime, String mainCabCode, String code, String param) {
        HashMap map = new HashMap(){{put("title","按存入来源(设备)");}};
        List<HashMap> list = new ArrayList();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            list.addAll(tempStorageReportDao.midSecondChart(start,end,mainCabCode,code,param));
        }
        if (type.equals("0")){
            list.addAll(tempStorageReportDao.midSecondChart(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code,param));
        }else if (type.equals("1")){
            list.addAll(tempStorageReportDao.midSecondChart(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code,param));
        }else if (type.equals("2")){
            list.addAll(tempStorageReportDao.midSecondChart(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code,param));
        }
        map.put("data",list);
        return JsonResult.success("查询成功",map);
    }

    @Override
    public JsonResult midThirdChart(String type, String startTime, String endTime, String mainCabCode, String code, String param) {
        HashMap map = new HashMap(){{put("title","按物料");}};
        List<HashMap> list = new ArrayList();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            list.addAll(tempStorageReportDao.midThirdChart(start,end,mainCabCode,code,param));
        }
        if (type.equals("0")){
            list.addAll(tempStorageReportDao.midThirdChart(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code,param));
        }else if (type.equals("1")){
            list.addAll(tempStorageReportDao.midThirdChart(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code,param));
        }else if (type.equals("2")){
            list.addAll(tempStorageReportDao.midThirdChart(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code,param));
        }
        map.put("data",list);
        return JsonResult.success("查询成功",map);
    }

    @Override
    public JsonResult midFourthChart(String type, String startTime, String endTime, String mainCabCode, String code, String param) {
        HashMap map = new HashMap(){{put("title","使用寿命对比");}};
        List<HashMap> list = new ArrayList();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            list.addAll(tempStorageReportDao.midFourthChart(start,end,mainCabCode,code,param));
        }
        if (type.equals("0")){
            list.addAll(tempStorageReportDao.midFourthChart(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code,param));
        }else if (type.equals("1")){
            list.addAll(tempStorageReportDao.midFourthChart(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code,param));
        }else if (type.equals("2")){
            list.addAll(tempStorageReportDao.midFourthChart(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code,param));
        }
        map.put("data",list);
        return JsonResult.success("查询成功",map);
    }

    @Override
    public JsonResult midFifthChart(String type, String startTime, String endTime, String mainCabCode, String code) {
        HashMap map = new HashMap(){{put("title","物料呆滞时长排名(单位:天)");}};
        List<HashMap> list = new ArrayList();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            list.addAll(tempStorageReportDao.midFifthChart(start,end,mainCabCode,code));
        }
        if (type.equals("0")){
            list.addAll(tempStorageReportDao.midFifthChart(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
        }else if (type.equals("1")){
            list.addAll(tempStorageReportDao.midFifthChart(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
        }else if (type.equals("2")){
            list.addAll(tempStorageReportDao.midFifthChart(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
        }
        map.put("data",list);
        return JsonResult.success("查询成功",map);
    }

    @Override
    public PageResult data(Integer page, Integer limit, String sort, String order, String startTime, String endTime, String type,String mainCabCode) {
        if(StringUtils.isNotBlank(type)){
            if (type.equals("0")){
                Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);
                Map queryParam=new HashMap();
                queryParam.put("sort", StrUtil.toUnderlineCase(sort));
                queryParam.put("order",order);
                queryParam.put("startCreateTime",ReportUtil.dayUtil().get(0).toString());
                queryParam.put("endCreateTime",ReportUtil.dayUtil().get(1).toString());
                queryParam.put("flag","report");
                queryParam.put("mainCabCode",mainCabCode);
                IPage<StorageInventoryInoutVO> list = storageInventoryInoutService.pageList(pg,queryParam);
                return new PageResult<>(list.getRecords(), list.getTotal());
            }else if (type.equals("1")){
                Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);
                Map queryParam=new HashMap();
                queryParam.put("sort",StrUtil.toUnderlineCase(sort));
                queryParam.put("order",order);
                queryParam.put("startCreateTime",ReportUtil.weekUtil().get(0).toString());
                queryParam.put("endCreateTime",ReportUtil.weekUtil().get(1).toString());
                queryParam.put("flag","report");
                queryParam.put("mainCabCode",mainCabCode);
                IPage<StorageInventoryInoutVO> list = storageInventoryInoutService.pageList(pg,queryParam);
                return new PageResult<>(list.getRecords(), list.getTotal());
            }else{
                Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);
                Map queryParam=new HashMap();
                queryParam.put("sort",StrUtil.toUnderlineCase(sort));
                queryParam.put("order",order);
                queryParam.put("startCreateTime",ReportUtil.dateUtil().get(0).toString());
                queryParam.put("endCreateTime",ReportUtil.dateUtil().get(1).toString());
                queryParam.put("flag","report");
                queryParam.put("mainCabCode",mainCabCode);
                IPage<StorageInventoryInoutVO> list = storageInventoryInoutService.pageList(pg,queryParam);
                return new PageResult<>(list.getRecords(), list.getTotal());
            }
        }
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);
            Map queryParam=new HashMap();
            queryParam.put("sort",StrUtil.toUnderlineCase(sort));
            queryParam.put("order",order);
            queryParam.put("startCreateTime",start.toString());
            queryParam.put("endCreateTime",end.toString());
            queryParam.put("flag","report");
            queryParam.put("mainCabCode",mainCabCode);
            IPage<StorageInventoryInoutVO> list = storageInventoryInoutService.pageList(pg,queryParam);
            return new PageResult<>(list.getRecords(), list.getTotal());
        }
        return null;
    }
}
